<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索框 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" @clear="getOrderList" clearable>
            <el-button @click="getOrderList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList" border stripe style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80px"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="80px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'" type="success">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80px"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="200px">
          <template slot-scope="scope">{{ scope.row.create_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <!-- 修改地址按钮 -->
            <el-tooltip :enterable="false" effect="dark" content="收货地址" placement="top">
              <el-button
                @click="showAddressDialog"
                type="primary"
                size="mini"
                icon="el-icon-position"
              ></el-button>
            </el-tooltip>
            <!-- 编辑订单状态按钮 -->
            <el-tooltip :enterable="false" effect="dark" content="订单状态" placement="top">
              <el-button
                @click="showOrderDialog(scope.row.order_id)"
                type="danger"
                size="mini"
                icon="el-icon-s-order"
              ></el-button>
            </el-tooltip>
            <!-- 查询物流进度按钮 -->
            <el-tooltip :enterable="false" effect="dark" content="物流进度" placement="top">
              <el-button
                @click="showExpressDialog"
                type="success"
                size="mini"
                icon="el-icon-location"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 修改订单地址对话框 仅做省市区联动的选择器效果，实际不发起请求更改地址-->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      @close="addressFormClosed"
      width="50%"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="cityProps"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑订单状态对话框 -->
    <el-dialog
      title="编辑订单状态"
      :visible.sync="orderDialogVisible"
      @close="orderFormClosed"
      width="50%"
    >
      <el-form :model="orderForm" ref="orderFormRef" label-width="100px">
        <!-- 订单编号 -->
        <el-form-item label="订单编号：">
          <el-input v-model="orderForm.order_number" disabled></el-input>
        </el-form-item>
         <!-- 下单时间 -->
        <el-form-item label="下单时间：">
          <el-input :value="orderForm.create_time | dateFormat" disabled></el-input>
        </el-form-item>
        <!-- 订单价格 -->
        <el-form-item label="订单价格">
          <el-input v-model="orderForm.order_price"></el-input>
        </el-form-item>
        <!-- 订单支付方式 -->
        <el-form-item label="支付方式：">
          <el-radio-group v-model="orderForm.order_pay">
            <el-radio :label="'0'">未支付</el-radio>
            <el-radio :label="'1'">微信</el-radio>
            <el-radio :label="'2'">支付宝</el-radio>
            <el-radio :label="'3'">银行卡</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 订单是否发货 -->
        <el-form-item label="是否发货：">
          <el-switch
            v-model="orderForm.is_send"
            active-value="1"
            inactive-value="0"
            active-text="已发货"
            inactive-text="未发货"
          ></el-switch>
        </el-form-item>
        <!-- 订单支付状态 -->
        <el-form-item label="支付状态：">
          <el-switch
            v-model="orderForm.pay_status"
            active-value="1"
            inactive-value="0"
            active-text="已支付"
            inactive-text="未支付"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="orderStateEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="expressDialogVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in expressInfo"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// 导入省市区/县数据
import cityData from './citydata'

export default {
  data() {
    return {
      // 订单数据
      orderList: [],
      // 查询信息
      queryInfo: {
        query: '', // 后台并没有处理查询关键字
        pagenum: 1,
        pagesize: 10
      },
      // 订单数据总数量
      total: 0,
      // 控制是否显示编辑地址对话框
      addressDialogVisible: false,
      // 省市区/县数据
      cityData,
      // 配置省市区/县
      cityProps: {
        expandTrigger: 'hover'
      },
      // 编辑地址表单
      addressForm: {
        address1: [],
        address2: ''
      },
      // 地址表单验证规则
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'change' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      // 物流信息
      expressInfo: [],
      // 控制是否显示物流进度对话框
      expressDialogVisible: false,
      // 控制是否显示编辑订单状态对话框
      orderDialogVisible: false,
      // 订单编辑表单
      orderForm: {}
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('查询订单数据失败')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 监听每页显示数据条数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 监听当前页码
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 点击修改收货地址按钮，显示编辑地址对话框
    showAddressDialog() {
      this.addressDialogVisible = true
    },
    // 关闭编辑地址对话框，清空地址表单
    addressFormClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 点击物流信息按钮，显示物流进度对话框
    async showExpressDialog() {
      // 发送请求，获取物流信息数据
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('查询物流信息失败')
      }
      console.log(res.data)
      this.expressInfo = res.data
      this.expressDialogVisible = true
    },
    // 点击修改订单按钮，显示编辑订单状态对话框
    async showOrderDialog(id) {
      const { data: res } = await this.$http.get(`orders/${id}`)
      // 服务器端返回的is_send是字符串：'是'、'否'，需要转换成'1'、'0'
      if (res.data.is_send === '是') {
        res.data.is_send = '1'
      } else {
        res.data.is_send = '0'
      }
      this.orderForm = res.data
      console.log(this.orderForm)
      // 显示订单编辑对话框
      this.orderDialogVisible = true
    },
    // 提交订单编辑表单，完成订单状态的编辑
    async orderStateEdit() {
      console.log(this.orderForm.order_id)
      const newForm = {}
      newForm.is_send = this.orderForm.is_send
      // 接口返回提示订单价格不能为空
      newForm.order_price = this.orderForm.order_price
      newForm.order_pay = this.orderForm.order_pay
      // 无论pay_status是字符串还是数字型提交，后台返回的永远是字符串0
      newForm.pay_status = this.orderForm.pay_status
      const { data: res } = await this.$http.put(`orders/${this.orderForm.order_id}`, newForm)
      console.log(res)
      if (res.meta.status !== 201) {
        return this.$message.error('更新订单状态失败')
      }
      this.$message.success('更新订单状态成功')
      // 刷新订单列表
      this.getOrderList()
      this.orderDialogVisible = false
    },
    // 关闭订单编辑对话框，清空表单
    orderFormClosed() {
      this.$refs.orderFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
